#===============================================================================
# MPR Master
# Arnold & Schulz: "Reaching For The Threshold"
# Chris Arnold, University of Birmingham, October 2025
#===============================================================================

# Dependencies
# For full sessionInfo() see bottom of the script
# R version 4.4.1 (2024-06-14)
# Platform: aarch64-apple-darwin20
# Running under: macOS 15.6.1

# 0 Housekeeping ===============================================================
start_time <- Sys.time()
# Set wd
setwd(getwd())

# Libraries
library(survival)
library(foreign)
library(readstata13)
library(ranger)
library(ggplot2)
library(dplyr)
library(ggfortify)
library(MatchIt)
library(clarify)
library(cem) # make sure quartz is installed on MacOS
library(stargazer)
library(fastDummies)
library(VennDiagram)
library(meta)
library(lattice)
library(latticeExtra)
library(stringi)
library(quanteda)
library(pdftools)
library(tesseract)
library(httr)

# install.packages("BiocManager")
# BiocManager::install("limma")
library(limma)

# Colours
# Viridis Colours
library(viridisLite)
library(viridis)

# Predefined Cardiff colours
# primary
cardiffred <- rgb(211,55,74, maxColorValue = 255)
cardiffblack <- rgb(35,21,32, maxColorValue = 255)
cardiffgrey <- rgb(47,68,78, maxColorValue = 255)
cardiffgold <- rgb(189,158,94, maxColorValue = 255)
# secondary 
cardiffblue <- rgb(21,44,81, maxColorValue = 255)
cardiffblue2 <- rgb(21,44,81, alpha = 140, maxColorValue = 255)
cardiffpurple1 <- rgb(29,15,51, maxColorValue = 255)
cardiffpurple2 <- rgb(60,44,89, maxColorValue = 255)
# Transparent colours
library(grDevices)
cardiffblue.t <- adjustcolor(cardiffblue, alpha.f = 0.5)
cardiffred.t <- adjustcolor(cardiffred, alpha.f = 0.5)
cardiffgold.t <- adjustcolor(cardiffgold, alpha.f = 0.5)


# Flag to run the whole code (1st run) or, instead, iterate more quickly with 
# saved results for re-runs
runall <- TRUE

# Flag for no run
run.DSM.LLM <- FALSE

# Helper files
source("helpers_data.r")
source("helpers_matching.r")
source("helpers_causal_effect.r")


# 1 Read Data ==================================================================
# Raw data we collected
dat <- read.dta13("dat_mtdsg_18april2019.dta")

# This is for parsing the original .pdf of treaty text from the archives
# and then annotating all with ChatGPT
# Do not run, since original .pdfs are not on the repo for file size reasons.
# Get in touch with Chris Arnold if you want to run this.
if (run.DSM.LLM) source('annotating_DSM.r')

# Further data processing
source("data_management.r")


# 2 Describing MPRs ============================================================
# Generates descriptive figures and tables of the MPRs
source("descriptive_data_analysis.r")


# 3 MPRs influence on participating in multilateral agreements =================

# 3.1 Hyp 2a ------------
# How do MPRs influence the entry into force of agreements? 
source('analyse_entry_into_force.r')

# 3.2 Hyp 1 and Hyp 2b ------------
# How do MPRs condition ratification after the threshold is met?

# Read the data and add some further covariates at individual country level
source('data_management_ind_level.r')
# Some of the robustess checks
source('ce_of_mpr_some_robustness.r')

# Updated utils based on review
source('helpers_causal_effect_2.r')
# Read the data and add some further covariates at individual country level
source('data_management_ind_level.r')
# Main results and further robustness
if (runall) source('ce_of_mpr_2.r')



# Time keeping
end_time <- Sys.time()
cat("Overall runtime on Chris' computer: 18.11 minutes \n")
cat("Overall runtime on your computer:", 
    round(end_time - start_time, 2), "minutes\n")







# Full sessionInfo()

# R version 4.4.1 (2024-06-14)
# Platform: aarch64-apple-darwin20
# Running under: macOS 15.6.1
# 
# Matrix products: default
# BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
# LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
# 
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# time zone: Europe/London
# tzcode source: internal
# 
# attached base packages:
#   [1] grid      tcltk     stats     graphics  grDevices utils    
# [7] datasets  methods   base     
# 
# other attached packages:
#   [1] limma_3.62.2        meta_8.1-0          metadat_1.4-0      
# [4] VennDiagram_1.7.3   futile.logger_1.4.3 fastDummies_1.7.5  
# [7] stargazer_5.2.3     cem_1.1.31          lattice_0.22-6     
# [10] clarify_0.2.1       MatchIt_4.7.1       ggfortify_0.4.17   
# [13] dplyr_1.1.4         ggplot2_3.5.2       ranger_0.17.0      
# [16] readstata13_0.11.0  foreign_0.8-86      survival_3.6-4     
# 
# loaded via a namespace (and not attached):
#   [1] gtable_0.3.6         CompQuadForm_1.4.3  
# [3] tzdb_0.5.0           mathjaxr_1.8-0      
# [5] numDeriv_2016.8-1.1  vctrs_0.6.5         
# [7] tools_4.4.1          Rdpack_2.6.4        
# [9] generics_0.1.3       tibble_3.2.1        
# [11] pkgconfig_2.0.3      Matrix_1.7-0        
# [13] RColorBrewer_1.1-3   lifecycle_1.0.4     
# [15] compiler_4.4.1       farver_2.1.2        
# [17] stringr_1.5.1        statmod_1.5.0       
# [19] combinat_0.0-8       pillar_1.10.2       
# [21] nloptr_2.2.1         tidyr_1.3.1         
# [23] MASS_7.3-60.2        reformulas_0.4.1    
# [25] boot_1.3-30          nlme_3.1-164        
# [27] tidyselect_1.2.1     digest_0.6.37       
# [29] stringi_1.8.7        purrr_1.0.4         
# [31] splines_4.4.1        colorspace_2.1-1    
# [33] cli_3.6.5            metafor_4.8-0       
# [35] magrittr_2.0.3       randomForest_4.7-1.2
# [37] readr_2.1.5          withr_3.0.2         
# [39] scales_1.4.0         backports_1.5.0     
# [41] lambda.r_1.2.4       lme4_1.1-37         
# [43] gridExtra_2.3        hms_1.1.3           
# [45] rbibutils_2.3        rlang_1.1.6         
# [47] futile.options_1.0.1 Rcpp_1.0.14         
# [49] glue_1.8.0           xml2_1.3.8          
# [51] formatR_1.14         rstudioapi_0.17.1   
# [53] minqa_1.2.8          R6_2.6.1     


























